home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14541 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: solon.com!not-for-mail
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: fflush(stdin) - not guaranteed to work?
  5. Date: 15 Apr 1996 13:12:10 -0500
  6. Organization: systems hk
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4ku3hq$52g@solutions.solon.com>
  10. References: <4ksjpn$rjt@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  13.  
  14. gohel@csee.usf.edu (Himanshu Gohel) wrote:
  15. [snip]
  16. >
  17. >What is the best way to get rid of the '\n' from the input buffer after
  18. >a scanf() statement?  In the same book on page 263 I've seen the following
  19. >format used in an fscanf() format specifier:
  20. >
  21.  
  22. Himanshu,
  23. I would recommend the following:
  24.  
  25.   char  *pnl;
  26.   char  inpBuf[MAXBUF];
  27.   FILE  *strm;
  28.   ...
  29.   ...
  30.   while( fgets(inpBuf,sizeof(inpBuf),strm) ) {
  31.     ...
  32.     if( (pnl=strchr(inpBuf,'\n')) )  /* truncate the newline */
  33.       *pnl = '\000';
  34.     ...
  35.     sscanf( inpBuf,... );
  36.     ...
  37.   }
  38.  
  39. Yours, Geoff Houck
  40.